//
//  YSForgetPwdCtrl.m
//  Yueshi
//
//  Created by Northwan on 15/7/24.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  忘记密码

#import "YSForgetPwdCtrl.h"
#import "YSMineDataTool.h"


@interface YSForgetPwdCtrl ()
@property (assign, nonatomic)NSTimer *countDownTimer;
@property (assign, nonatomic)NSInteger currentTime;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *verifyTextField;
@property (weak, nonatomic) IBOutlet UIButton *sendCodeBtn;
@property (strong, nonatomic) UIImageView *bgImgView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation YSForgetPwdCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentTime = 60;
    self.fd_prefersNavigationBarHidden = YES;
    _bgImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, SCREENW, SCREENH)];
    [self.view insertSubview:_bgImgView belowSubview:self.scrollView];
    
    if(SCREENH==480){// 4s 4
        self.bgImgView.image = [UIImage imageNamed:@"bg_login_4.jpg"];
    }else if(SCREENH==568){// 5s 5
        self.bgImgView.image = [UIImage imageNamed:@"bg_login_5.jpg"];
    }else if(SCREENH==667){// 6
        self.bgImgView.image = [UIImage imageNamed:@"bg_login_6.jpg"];
    }else if(SCREENH==736){// 6 plus
        self.bgImgView.image = [UIImage imageNamed:@"bg_login_6p.jpg"];
    }

    
    [UIUtil dealPlaceHolderColor:self.phoneTextField color:[UIColor whiteColor]];
    [UIUtil dealPlaceHolderColor:self.verifyTextField color:[UIColor whiteColor]];
    [UIUtil dealPlaceHolderColor:self.pwdTextField color:[UIColor whiteColor]];
    
    self.phoneTextField.tintColor = [UIColor whiteColor];
    self.verifyTextField.tintColor = [UIColor whiteColor];
    self.pwdTextField.tintColor = [UIColor whiteColor];
}

-(BOOL)shouldStatusBarLightContent{
    return YES;
}

- (IBAction)tapSender:(id)sender {
    [self.view endEditing:YES];
}

- (IBAction)tapAction:(UIButton *)sender {
    switch (sender.tag) {
        case 0:
            [self.navigationController popViewControllerAnimated:YES];
            break;
        case 2:{
            if(ISBLANK(self.pwdTextField.text)){
                ShowTips(@"新密码为空");
                return;
            }
            if(ISBLANK(self.phoneTextField.text)){
                ShowTips(@"手机号为空");
                return;
            }
            if(ISBLANK(self.verifyTextField.text)){
                ShowTips(@"验证码为空");
                return;
            }
            
            [YSMineDataTool forgetWithParams:self.phoneTextField.text password:self.pwdTextField.text validateCode:self.verifyTextField.text success:^(BaseResponse *result) {
                if(result.isSucc){
                    ShowTips(@"修改密码成功")
                    [self.navigationController popViewControllerAnimated:YES];
                }else{
                    ShowEnterTips(result.data);
                }
            } failure:^(NSError *error) {
                ShowTips(kRequestFailure);
            }];
            break;
        }
        case 3:{//获取验证码
            if(ISBLANK(self.phoneTextField.text)){
                ShowTips(@"手机号为空");
                return;
            }
            [YSMineDataTool sendCodeWithParams:self.phoneTextField.text type:1 success:^(BaseResponse *result) {
                if(result.isSucc){
                    [self startCountDown];
                }else{
                   ShowEnterTips(result.data);
                }
            } failure:^(NSError *error) {
                ShowTips(kRequestFailure);
            }];
            break;
        }
        default:
            break;
    }
}

- (void)startCountDown {
    //处理倒计时
    NSString* time_str = [NSString stringWithFormat:@"%ld",self.currentTime];
    _countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:_countDownTimer forMode:NSDefaultRunLoopMode];
    [_sendCodeBtn setEnabled:NO];
    [_sendCodeBtn setTitle:time_str forState:UIControlStateDisabled];
}

-(void)timeFireMethod{
    if(self.currentTime <= 0){
        [_countDownTimer invalidate];
        [_sendCodeBtn setEnabled:YES];
        [_sendCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    }else{
        self.currentTime--;
        NSString *timeStr = [NSString stringWithFormat:@"%ld",self.currentTime];
        [_sendCodeBtn setTitle:timeStr forState:UIControlStateDisabled];
    }
}

- (void)dealloc
{
    _countDownTimer = nil;
}


@end
